/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2016-12-09 17:17:09
 * @Description: 
 */

function queryString(item) {
    var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
    return svalue ? svalue[1] : svalue;
};

function getApiAccessUrl(url) {
    var mock = queryString('mock'),
        sid = queryString('sid');
    if (mock) {
        apiAccessUrl = url + 's' + sid + '/';
    } else {
        apiAccessUrl = url;
    }
    return apiAccessUrl;
};

function getLocale() {
    var locale = getCookie('siteLocale');
    return locale == '' ? 'en' : local;
};

function getCookie(name) {
    var search = name + "=",
        offset, end;
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) {
                end = document.cookie.length;
            };
            return decodeURI(document.cookie.substring(offset, end));
        }
    }
    return "";
};

seajs.config({
    alias: {
        "baseLang": remoteUrl + "/base/i18n/" + getLocale() + ".js",
        "lang": remoteUrl + "/" + engine + "/i18n/" + getLocale() + ".js",
        "$": "tools/jQuery/jquery-1.12.4.js",
        "underscore": "tools/underscore/underscore-1.8.3.js",
        "director": "tools/director/director-1.2.6.js",
        "util": "tools/utils/util.js",
        "baseShare": remoteUrl + "/base/share",
        "baseConfig": remoteUrl + "/base/config",
        "config": remoteUrl + "/" + engine + "/config",
    },

    preload: [
        '$', 'director', 'underscore'
    ],

    paths: {
        'tools': remoteUrl + '/base/tools'
    },

    vars: {
        'base': remoteUrl + '/base',
        'site': remoteUrl + '/' + engine,
        'locale': getLocale(),
        'apiUrl': apiUrl,
        'apiAccessUrl': getApiAccessUrl(apiUrl),
    },

    debug: true,

    base: remoteUrl,

    charset: 'utf-8',

    'map': [
        [/^(.*\.(?:css|js|tpl))(.*)$/i, '$1?v=' + version]
    ],
});

seajs.use(['$', 'underscore', 'director', 'lang', 'baseLang', 'baseShare', 'baseConfig', 'config', 'util'], function($, underscore, director, lang, baseLang, baseShare, baseConfig, config, util) {
    window.Router = director;
    window._ = underscore;
    window.$ = $;
    window.Util = util;
    window.Share = baseShare;
    window.Lang = _.extend(baseLang, lang);
    window.Config = _.extend(baseConfig, config);
    window.MemoryStore = false;
    if (!window.localStorage) {
        window.MemoryStore = true;
        window.MemoryLocalStorage = {};
        window.MemorySessionStorage = {};
    }
    seajs.use(remoteUrl + "/base/main");
});
